package com.igeek.oa.service.impl;

import cn.hutool.core.date.DateTime;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.igeek.oa.entity.Employee;
import com.igeek.oa.entity.Purchase;
import com.igeek.oa.entity.PurchaseGoods;
import com.igeek.oa.mapper.PurchaseGoodsMapper;
import com.igeek.oa.mapper.PurchaseMapper;
import com.igeek.oa.service.IPurchaseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.security.PrivateKey;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * <p>
 * 采购申请表 服务实现类
 * </p>
 *
 * @author author
 * @since 2024-08-30
 */
@Service
public class PurchaseServiceImpl extends ServiceImpl<PurchaseMapper, Purchase> implements IPurchaseService {
    @Autowired
    private PurchaseMapper purchaseMapper;
    @Autowired
    private PurchaseGoodsMapper purchaseGoodsMapper;
    @Override
    public void addPurchase(String shenQingPerNo,String deptId,String shenQingMoney, List<PurchaseGoods> shopMess) {
        String format = "yyyy-MM-dd HH:mm:ss";
        // 获取表示当前时间的LocalDateTime
        LocalDateTime localDateTime = LocalDateTime.now();
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(format);
        // 将LocalDateTime对象转成指定格式的字符串
        String time = localDateTime.format(dateTimeFormatter);
        LocalDateTime parse = LocalDateTime.parse(time, dateTimeFormatter);
        Purchase purchase = new Purchase();
        purchase.setState("0");
        purchase.setDeptId(deptId);
        purchase.setMoney(new BigDecimal(shenQingMoney));
        purchase.setEmpId(shenQingPerNo);
        purchase.setApplyTime(parse);
        purchaseMapper.insert(purchase);
        for (int i = 0; i < shopMess.size(); i++) {
            PurchaseGoods purchaseGoods1 = new PurchaseGoods();
            purchaseGoods1.setName(shopMess.get(i).getName());
            purchaseGoods1.setCount(shopMess.get(i).getCount());
            purchaseGoods1.setUnitPrice(shopMess.get(i).getUnitPrice());
            purchaseGoods1.setPurchaseId(purchase.getId());
            purchaseGoodsMapper.insert(purchaseGoods1);
        }


    }
}
